package com.titanrise.crm.dao;

import com.titanrise.crm.domain.model.*;
import com.titanrise.crm.environment.annotation.Autowired;
import com.titanrise.crm.environment.annotation.Repository;
import com.titanrise.crm.environment.ioc.SingletonPoolUtil;
import com.titanrise.crm.mapper.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description:
 * @Author: pront
 * @Time:2024-11-25 21:20
 */
@Repository
public class CustomerDao {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private static UserMapper userMapper;

    @Autowired
    private static RoleMapper roleMapper;

    @Autowired
    private ClientSalesRelationshipsMapper clientSalesRelationshipsMapper;

    @Autowired
    private MessagesMapper messagesMapper;


    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private TradeMapper tradeMapper;

    /**
     * 查询所有商品
     * @return 商品集合
     */
    public List<Product> showAllProducts() {
        return customerMapper.selectAll();
    }

    public static User selectUserInfo(String uid) {
        return userMapper.selectById(uid);
    }

    public User selectUserById(String uid) {
        return userMapper.selectById(uid);
    }

    public Role selectRoleByUid(String uid) {
        return roleMapper.selectByUId(uid);
    }

    public String selectSalesIdByClientId(String clientId) {
       return clientSalesRelationshipsMapper.selectSalesIdByClientId(clientId);
    }

    public int sendMessage(Messages message) {
        return SingletonPoolUtil.getBean(RootDao.class).sendMessage(message);
    }

    public List<Messages> selectAllMsgByReceiverId(String receiverId) {
        return messagesMapper.selectAllMsgByReceiverId(receiverId);
    }

    public int updateReadedMes(String messageId, LocalDateTime readTime) {
        return messagesMapper.updateStatusById(messageId, readTime);
    }

    public int deleteMsgById(String messageId) {
        return messagesMapper.deleteById(messageId);
    }

    public Product selectProductByProductId(String productId) {
        return productMapper.selectById(productId);
    }

    public int updateProductStock(String productId, int newStock) {
        return productMapper.updateStockById(productId, newStock);
    }

    public String exitTradeCount(String clientId, String salesId, String goodsName) {
        return tradeMapper.selectTradeCountByClientIdAndSalesIdAndSkuName(clientId, salesId, goodsName);
    }

    public int exitTradeCount(String clientId) {
        return tradeMapper.selectTradeCountByClientId(clientId);
    }

    public int updateTradeStatusById(String tradeId, int status, String updateName, BigDecimal price) {
        return tradeMapper.updateTradeStatusById(tradeId, status, updateName, price);
    }

    public int insertTradeCount(Trade trade) {
        return tradeMapper.insertOne(trade);
    }

    public int updateTradeNodeByTradeId(String tradeId, String note) {
        return tradeMapper.updateTradeNodeByTradeId(tradeId, note);
    }

    public int deleteRelationByUid(String clientId) {
        return clientSalesRelationshipsMapper.deleteByClientId(clientId);
    }

    public int deleteTradeByClientUid(String uid) {
        return tradeMapper.deleteTradeByClientUid(uid);
    }

    public int deleteMsgByUid(String uid) {
        return messagesMapper.deleteByReceiverIdOrSenderId(uid);
    }

    public int deleteRoleByUid(String uid) {
        return roleMapper.deleteByUid(uid);
    }

    public int deleteUserById(String uid) {
        return userMapper.deleteById(uid);
    }

    public int updateRoleUsnaPaswByUid(String uid, String username, String password) {
        return roleMapper.updateRoleUsnaPaswByUid(uid, username, password);
    }

    public Trade selectTradeById(String tradeId) {
        return tradeMapper.selectById(tradeId);
    }

    public int updateUser(User user) {
        return userMapper.updateOne(user);
    }

    public List<Trade> selectAllConfirmDealTradeByClientId(String client_id) {
        return tradeMapper.selectAllConfirmDealTradeByClientId(client_id);
    }

    public List<Trade> selectAllTradeByClientId(String uid) {
        return tradeMapper.selectAllTradeByClientId(uid);
    }

    public Product selectByIdProduct(String id){
        return customerMapper.selectByIdProduct(id);
    }
}
